package cn.com.HiSting1990.MyBasis;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class myThread {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Main方法开始...");

//        myThreads();
        myThreadExecutors();

        System.out.println("Main方法结束...");
    }

    private static void myThreads() throws InterruptedException {
        System.out.println("我们开始主线程的代码");
        int a = 0;
        while (a<10){
            System.out.println("主线程1  "+a++);
        }

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (i<20){
                    System.out.println("支线程  "+i++);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
        int b = 0;
        while (b<20){
            System.out.println("主线程2  "+b++);
            Thread.sleep(1);
        }
    }

    private static void myThreadExecutors() throws InterruptedException {
        System.out.println("我们开始主线程的代码");
        int a = 0;
        while (a<10){
            System.out.println("主线程1  "+a++);
        }

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 100, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10));
        //业务代码
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (i<20){
                    System.out.println("支线程"+Thread.currentThread().getName()+"----"+i++);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        threadPoolExecutor.execute(runnable);
        int b = 0;
        while (b<20){
            System.out.println("主线程2  "+b++);
            Thread.sleep(10);
        }
    }
}
